package com.fortune.service.ananlysis;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.DateFormatUtils;
import com.fortune.dal.model.Bank;
import com.fortune.service.BankService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.mail.Message;
import java.util.Date;

@Component
public class AnanlysisFactory {

    protected static final Logger logger = LoggerFactory.getLogger(AnanlysisFactory.class);

    @Autowired
    private AnanlysisService cmbCreditCardBillMailAnanlysis;

    @Autowired
    private AnanlysisService nbcbCreditCardBillMailAnanlysis;

    @Autowired
    private AnanlysisService icbcCreditCardBillMailAnanlysis;

    @Autowired
    private AnanlysisService pabCreditCardBillMailAnanlysis;

    @Autowired
    private AnanlysisService spdbCreditCardBillMailAnanlysis;

    @Autowired
    private AnanlysisService boscCreditCardBillMailAnanlysis;

    @Autowired
    private BankService bankService;

    public AnanlysisService getAnanlysis(Message me) throws BusinessException {
        try {
            String title = me.getSubject();//获取邮箱标题
            Date d = me.getReceivedDate() != null ? me.getReceivedDate() : me.getSentDate();
            String str = d != null ? DateFormatUtils.yyyy_MM_dd.format(d) : "";
            logger.info(str + "邮件标题：" + title);
            //   宁波银行信用卡对账单
            if ("宁波银行信用卡对账单".equals(title)) {
                return nbcbCreditCardBillMailAnanlysis;
            } else if ("平安信用卡电子账单".equals(title)) {
                return pabCreditCardBillMailAnanlysis;
            } else if ("招商银行信用卡账单".equals(title)) {
                return cmbCreditCardBillMailAnanlysis;
            } else if ("中国工商银行客户对账单(ICBC Peony Card Bank Statement)".equals(title)) {
                return icbcCreditCardBillMailAnanlysis;
            } else if ("浦发银行-信用卡电子账单".equals(title)){
                return cmbCreditCardBillMailAnanlysis;
            } else if ("上海银行2020年4月信用卡电子对账单".equals(title)){
                return cmbCreditCardBillMailAnanlysis;
            } else if ("兴业银行信用卡2020年11月电子账单".equals(title)){
                return cmbCreditCardBillMailAnanlysis;
            } else if ("兴业银行信用卡2020年11月电子账单".equals(title)){
                return cmbCreditCardBillMailAnanlysis;
            } else if ("兴业银行信用卡2020年11月电子账单".equals(title)){
                return cmbCreditCardBillMailAnanlysis;
            } else if ("兴业银行信用卡2020年11月电子账单".equals(title)){
                return cmbCreditCardBillMailAnanlysis;

            } else if (StringUtils.isNotBlank(title) && title.contains("招商银行信用卡电子账单")){
                return cmbCreditCardBillMailAnanlysis;
            }
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return null;
    }

    public Bank getBank(Message me) throws BusinessException {
        try {
            String title = me.getSubject();
            //   宁波银行信用卡对账单
            if ("宁波银行信用卡对账单".equals(title)) {
                return bankService.queryBankByName("宁波银行");
            } else if ("平安信用卡电子账单".equals(title)) {
                return bankService.queryBankByName("平安银行");
            } else if ("招商银行信用卡账单".equals(title)) {
                return bankService.queryBankByName("招商银行");
            } else if ("中国工商银行客户对账单(ICBC Peony Card Bank Statement)".equals(title)) {
                return bankService.queryBankByName("工商银行");
            }
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return null;
    }

}
